package com.hsck.order.kds.setup.fragment.cxzd

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.hjq.toast.Toaster
import com.hsck.order.kds.R
import com.hsck.order.kds.base.bean.CategoryBean
import com.hsck.order.kds.base.isnoam.network.livedata.LiveDataBus
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.databinding.FragmentCxjmBinding
import com.hsck.order.kds.setup.SetupActivity
import com.hsck.order.kds.setup.adapter.CxjmAdpter
import kotlinx.android.synthetic.main.common_head_black.*

/**
 * 厨显界面
 */
class CxjmFragment : BaseFragment<FragmentCxjmBinding>(), SetupActivity.UpdatableFragment {


    var type = ""
    var topFragment: Fragment? = null
    private val adapter by lazy { CxjmAdpter(R.layout.item_cxjm) }


    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentCxjmBinding {
        return FragmentCxjmBinding.inflate(inflater)
    }

    fun setBack() {
        if (topFragment != null) {
            val activity = requireActivity() as SetupActivity
            activity.switchFragment(topFragment!!)
        }
    }

    override fun loadDataOnce() {
        super.loadDataOnce()
        lin_head_black_back.setOnClickListener {
            setBack()
        }
        tv_head_black_title.text = "厨显界面"
        binding.apply {
            rvCxjm.layoutManager = GridLayoutManager(requireActivity(), 3)
            rvCxjm.adapter = adapter
            adapter.setMultipleChoice(R.id.cb_item_cxjm, true)
            val list = arrayListOf<CategoryBean>()
            for (i in 1..6) {
                list.add(CategoryBean("$i", true))
            }
            adapter.datas = list

            if (list.size > 0) {
                var name = ""
                list.forEach {
                    if (name==""){
                        name = it.tagName
                    }else{
                        name = "$name、${it.tagName}"
                    }
                }
                tvCxjmSelectName.text = "($name)"
            } else {
                tvCxjmSelectName.text = "无"
            }

            adapter.onItemClick = {
                val list = adapter.getSelect() as ArrayList<CategoryBean>
                if (list.size > 0) {
                    var name = ""
                    list.forEach {
                        if (name==""){
                            name = it.tagName
                        }else{
                            name = "$name、${it.tagName}"
                        }
                    }
                    tvCxjmSelectName.text = "($name)"
                } else {
                    tvCxjmSelectName.text = "无"
                }
            }

            //确认
            btnCxjmQr.setOnClickListener {
                val list = adapter.getSelect() as ArrayList<CategoryBean>
                if (list.size > 0) {
                    //给上级页面传值
                    LiveDataBus.with<ArrayList<CategoryBean>>("CxjmFragment").postData(list)
                    setBack()
                } else {
                    Toaster.show("请选择厨显界面")
                }
            }

        }
    }

    override fun onFragmentVisible() {
        //可见
        if (_binding != null) {

        }
    }

    override fun onFragmentInvisible() {
        //不可见

    }

}